package goubanjia

import (
	"github.com/PuerkitoBio/goquery"
	"github.com/ruoklive/proxypool/internal/ipgetter/base"
	"github.com/ruoklive/proxypool/pkg/models"
	"github.com/ruoklive/proxypool/pkg/register"
	"go.uber.org/zap"
	"io"
	"regexp"
	"strings"
)

func init() {
	register.Add(func() register.IPGetter {
		return New()
	})
}

type IPGetter struct {
	base.Base
}

func New() *IPGetter {
	g := &IPGetter{}
	g.Base = base.NewBase("http://www.goubanjia.com/",g.Parse,"GouBanJia")
	return g
}

func (g *IPGetter) Parse(body io.ReadCloser) (result []*models.IP) {
	doc, err := goquery.NewDocumentFromReader(body)
	if err != nil {
		g.Error("Parse fail!",zap.Error(err))
		return
	}

	doc.Find("body > div.container > div.section-header > div.row > div.container-fluid > div.row-fluid > div.span12 > tbody > tr").Each(func(_ int, s *goquery.Selection) {
		sf, _ := s.Find(".ip").Html()
		tee := regexp.MustCompile("<pstyle=\"display:none;\">.?.?</p>").ReplaceAllString(strings.Replace(sf, " ", "", -1), "")
		re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
		ip := models.NewIP()
		ip.Data = re.ReplaceAllString(tee, "")
		ip.Type1 = s.Find("td:nth-child(3) > a").Text()
		g.Info("GET IP",zap.String("ip",ip.String()))
		result = append(result, ip)
	})
	g.Info("done.")
	return nil
}
